﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AllegroIWI.pl.allegro.webapi;
using System.Drawing;
using System.Net;
using System.IO;

namespace AllegroIWI
{
	public class WebApi
	{
		public string webapiKey = "4544a107";
		public int versionKey = //1376386632; //testwebapi.pl
			1390830560; 
			//1390354735; //allegro.pl
		public int testCoutryCode = 228;
		public int countryCode = 1;

		public String SessionHandlePart;
		public long UserId;
		public long ServerTime;

		private AllegroWebApiService service = new AllegroWebApiService();

		public String SearchString = "alfa";
		public int SearchCount, SearchCountFeatured;
		public SearchResponseType[] results = null;
		public String[] excludedWords;
		public CategoriesStruct[] categories;

		public WebApi()
		{
			SessionHandlePart = service.doLogin("iwiofdoom@gazeta.pl", "AllegroOfDoom1", testCoutryCode, webapiKey, versionKey, out UserId, out ServerTime);
		}

		public WebApi(string webapiKey, string login, string password)
		{
			this.webapiKey = webapiKey;
			SessionHandlePart = service.doLogin(login, password, countryCode, webapiKey, versionKey, out UserId, out ServerTime);
		}

		public List<SearchResult> SearchResults = null;
		public void searchMe()
		{
			SearchOptType searchOptType = new SearchOptType();
			searchOptType.searchstring = SearchString;

			SearchCount = service.doSearch(SessionHandlePart, searchOptType, out SearchCountFeatured, out results, out excludedWords, out categories);
			SearchResults = new List<SearchResult>();

			for (int i = 0; i < results.Length && i < 100; i++)
			{
				SearchResult result = new SearchResult();
				result.Id = results[i].sitid;
				result.Name = results[i].sitname;
				//System.Console.WriteLine(results[i].sitname);
	
				long[] ItemsIdArray = new long[1];
				ItemsIdArray[0] = results[i].sitid;	

				ItemInfoStruct[] ItemsInfos = null;
				long[] ArrayItemsNotFound;
				long[] ArrayItemsAdminKilled;
				
				ItemsInfos = service.doGetItemsInfo(SessionHandlePart, ItemsIdArray, 0, 1, 0, 0, 0, 0, out ArrayItemsNotFound, out ArrayItemsAdminKilled);

				if (ItemsInfos != null && ItemsInfos[0].itemimages != null && ItemsInfos[0].itemimages.Length > 0)
					result.Image = new ImageDecorator(getBitmapFromUrl(ItemsInfos[0].itemimages[0].imageurl));
				else
					result.Image = null;

				SearchResults.Add(result);
			}

		}

		public Bitmap getBitmapFromUrl(String url)
		{
			Stream stream = null;
			Bitmap bitmap = null;
			try
			{

				HttpWebRequest req;
				req = (HttpWebRequest)HttpWebRequest.Create(url);
				req.AllowWriteStreamBuffering = true;

				HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
	
				stream = resp.GetResponseStream();
				
				if (stream != null)
				{
					bitmap = new Bitmap(stream);
				}
			}
			catch (Exception ex)
			{

			}
			finally {
				if (stream != null)
					stream.Close();
			}
			return bitmap;
		}


	}
}
